package csce315.group10.androidapp;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class storyToEvent extends Activity{
	/* private instance variables */
	CustomApp appState;
	String story_name;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.storytoevent);
        appState = ((CustomApp)getApplicationContext());
        /* get the names of all the potential stories */
    	String story_names[] = new String[appState.GetPStories().size()];
    	for(int i = 0; i < appState.GetPStories().size(); i++)
    	{
    		story_names[i] = appState.GetPStories().get(i).get_story_name();
    	}
        Spinner spinner = (Spinner) findViewById(R.id.Transfer_story_dropdown);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item,story_names);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new StorySelectedListener());
    }
    
    public void okButtonHandler(View v){
    	/* return story name */
    	Intent intent = new Intent();
    	  intent.setClass(this, TransferPotentialStoryDialog.class);
    	  /* find and insert story into intent */
    	  for(int i = 0; i < appState.GetPStories().size(); i++)
    	  {
    		  if(appState.GetPStories().get(i).get_story_name() == story_name){
    			  intent.putExtra("story", appState.GetPStories().get(i));
    			  break;
    		  }
    	  }
    	  /* call dialog to get event info based on story info */
    	  startActivityForResult(intent, 0); 
    }
  
    @Override
	public void onActivityResult(int requestCode,int resultCode, Intent data){
    	/* if back button was pressed then return */
    	if(resultCode != Activity.RESULT_OK){
    		return;
    	}
		 setResult(Activity.RESULT_OK, data);
		 finish();
    }
    /* class that monitors the contact selector */
    public class StorySelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        	story_name = parent.getItemAtPosition(pos).toString();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
}